# watch_Preprocessor_NumDefined_then_User_h_MacroName.gs :  从 watch 预处理器宏定义个数变量NumDefined 到 打印 User.h 中 自定义宏名字 
# 执行:  gdb -x watch_Preprocessor_NumDefined_then_User_h_MacroName.gs    --args /pubx/llvm15-build/bin/clang    -c user_main.cpp -o user_main.o

set logging file macro_dev.log
set logging on

set print pretty on
set pagination off

set follow-exec-mode new
set follow-fork-mode child

#人工 从 Twine_union_Child_print.gs 中 应该去  函数 createPreprocessor 
break CompilerInstance.cpp:473
#断点1 clang::CompilerInstance::createPreprocessor ( ) at /pubx/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:473
commands 1
disable 1

frame 2
#2  0x000055555e12011f in clang::CompilerInstance::ExecuteAction (this=0x555563747760, Act=...) at /pubx/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1034
set $ciPtr=this
set $smPtr=& ( $ciPtr->getSourceManager() )

frame 0
#0  clang::CompilerInstance::createPreprocessor (this=0x555563747760, TUKind=clang::TU_Complete) at /pubx/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:473
set $boolPtr=(bool*)(malloc(sizeof(bool)))

#人工看出来, 变量 PP.get()->NumDefined 表示 定义的宏个数, 因此 当 该变量改变时 停下来
#断点2
watch -location PP.get()->NumDefined
	commands 2
	silent
	#因断点2 而 停下
	# clang::Preprocessor::HandleDefineDirective (this=0x55556375bcf0, DefineTok=..., ImmediatelyAfterHeaderGuard=false) at /pubx/llvm-project/clang/lib/Lex/PPDirectives.cpp:3027
	# 3027	  ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);

	set $location= DefineTok.getLocation() 
	set $fileNameCStr=$smPtr->getFilename($location).Data
	set $textCStr=$smPtr->getCharacterData($location,$boolPtr)
	
	printf "fileName:%s,  ",$fileNameCStr
	print $textCStr
	
	if $fileNameCStr != 0 && strcmp($fileNameCStr,"./User.h") == 0
		print "断点2 停止后 若发现感兴趣文件User.h 则停下 设置打印宏的新断点3"
		
		disable 2
		
		#在此处: 函数 HandleDefineDirective : llvm-project/clang/lib/Lex/PPDirectives.cpp:3027  ,  人工 next 执行了几行代码, 观察每个变量,  发现 第 3036 行的变量II表示宏名字
		break PPDirectives.cpp:3036
		commands 3
#clang::Preprocessor::HandleDefineDirective  at /pubx/llvm-project/clang/lib/Lex/PPDirectives.cpp:3036  :
#IdentifierInfo *II = MacroNameTok.getIdentifierInfo();  //第3033行
#// Issue a final pragma warning if we're defining a macro that was has been
#// undefined and is being redefined.
#if (!II->hasMacroDefinition() && II->hadMacroDefinition() && II->isFinal()) //第3036行
#	emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
			#打印宏名字
			set $macroNameCStr= II->Entry->getKey().Data
			print "macroName:%s\n",$macroNameCStr
			set $_fileNamCStr=$smPtr->getFilename(DefineTok.getLocation() ).Data
			
			if $_fileNamCStr != 0 && strcmp($_fileNamCStr,"./User.h") == 0
				print "断点3 停止后 发现感兴趣文件 User.h 并打印宏名字, 此时手工 continue 几次 可以看到 User.h中的后续定义的其他宏"
			else
				continue
				#断点3 停止后 非感兴趣文件 User.h 不需要停下
			end
			#if结束
		end
		#断点3 处理结束
		
		continue
		#断点2 停止后 发现感兴趣点后 不需要停下
	else
		continue
		#断点2 停止后 不需要停下
	end
	#if结束

	
	end
	#断点2 处理结束

continue
#断点1 停止后 不需要停下
end
#断点1 处理结束

run

#因断点1 而停下
